package com.example.userlogin.service;

//实现类

import com.example.userlogin.dao.PurchaseDao;
import com.example.userlogin.entity.Purchase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

//实现操作方法
public class PurchaseService implements PurchaseDao {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int add(Purchase purchase) {
//        创建SQL语句
        String sql = "insert into purchase values(null,?,?,?,?,?,?,?)";
//        调用方法实现
        Object[] args = {purchase.getSupplier(),purchase.getName(),purchase.getCategory(),purchase.getNumber(),
                purchase.getPrice(),purchase.getTime(),purchase.getAmount()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int delete(Purchase purchase) {
        String sql ="delete from purchase where Id=?";
        Object[] args={purchase.getId()};
        return jdbcTemplate.update(sql,args);
    }


    @Override
    public int update(Purchase purchase) {
        String sql ="update purchase set supplier=?,name=?,category=?,number=?,price=?,time=?,amount=? where Id=?";
        Object[] args={
                purchase.getSupplier(),purchase.getName(),purchase.getCategory(),
                purchase.getNumber(),purchase.getPrice(),purchase.getTime(),purchase.getAmount(),purchase.getId()
        };
        return jdbcTemplate.update(sql,args);
    }

    /**
     *
     * @param
     * @return
     */
    @Override
    public Purchase findOne(Purchase purchase) {
        String sql = "select * from purchase where Id="+purchase.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Purchase>(Purchase.class));
    }

    /**
     * 获取所有商品列表的方法
     * @return
     */
    @Override
    public List<Purchase> findAll() {
        String sql = "select * from purchase";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Purchase>(Purchase.class));
    }
}
